/**
 * Created by dywu on 2015/9/24.
 */
angular.module('preferential.user', [
])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('preferential.user', {
            url: '/preferential/list',
            templateUrl: usercenter_viewurl+"/preferential/list.html",
            resolve: {
                user_list : ['$stateParams','UserService',function($stateParams, UserService){
                    var page={
                        pageIndex:'',
                        pageSize:'',
                    }
                    return UserService.getcoupontemplateall(page).$promise;
                }]
            },
            controller: 'PreferentialController'
        })
        .state('preferential.userdetail', {
            url: '/preferential/detail/:coupontemplateid',
            templateUrl: usercenter_viewurl+"/preferential/detail.html",
            resolve: {
                coupontemplate : ['$stateParams','UserService', function($stateParams,UserService){
                    return UserService.getcoupontemplatebyid($stateParams.coupontemplateid).$promise;
                }]
            },
            controller: 'PreferentialDetailController'
        })
        .state('preferential.useradd', {
            url: '/preferential/add',
            templateUrl: usercenter_viewurl+"/preferential/add.html",
            controller: 'PreferentialAddController'
        })
        .state('preferential.userauth', {
            url: '/preferential/auth',
            templateUrl: usercenter_viewurl+"/preferential/auth_list.html",
            resolve: {
                user_list : ['UserMemberService',function(UserMemberService){
                    return UserMemberService.getauthlist(1).$promise;
                    
                }]
            },
            controller: 'PreferentialAuthController'
        })
}])

.controller('PreferentialController', ['$scope','UserService','user_list', function($scope, UserService,user_list) {
       console.log(user_list);
       $scope.users=user_list;
        console.log( $scope.users)
       $scope.remove = function(coupontemplateid) {
           var indextodelete = -1;
           for (var i=0;i<$scope.users.length;i++) {
               if ($scope.users[i].coupontemplateid == coupontemplateid) {
                   //从数组中删除这个id的�??
                   indextodelete = i;
                   break;
               }
           }
           UserService.remove(coupontemplateid).$promise.then(function (data) {
               if (indextodelete >= 0) {
                   $scope.users.splice(indextodelete, 1);
               }
               alert("删除优惠券成功", 1000);
           }, function (data) {
               alert("删除优惠券失败", 1000);
           })
        }
}])
.controller('PreferentialDetailController', ['$scope','$stateParams','UserService','coupontemplate',function($scope,$stateParams, UserService,coupontemplate) {
    $scope.coupontemplate=coupontemplate;
        console.log($scope.coupontemplate.coupontemplateid)
    $scope.modify=function(coupontemplate){
        UserService.update($scope.coupontemplate,function(data){
            console.log($scope.coupontemplate)
            alert("更新成功");
            history.go(-1);
        },function(data){
            alert(coupontemplate);
            console.log(data);
        })
    }
}])
.controller('PreferentialAddController', ['$scope','$state','UserService', function($scope,$state, UserService) {
        var d = new Date();
        $scope.start_time_show=d.getFullYear() + "-" + (d.getMonth()-1) + "-" + d.getDate();
        $scope.end_time_show=d.getFullYear() + "-" + (d.getMonth()+1) + "-" + (d.getDate()+1);

        var e = new Date($scope.end_time_show.replace(/-/g, '/'));
        var s = new Date($scope.start_time_show.replace(/-/g, '/'));
        $scope.start_time=s.getTime();
        $scope.end_time=e.getTime();
        $scope.end_time_show=d.getFullYear() + "-" + (d.getMonth()+1) + "-" + (d.getDate()+1);
        $('#datetimepicker').datetimepicker({
            format: "yyyy-MM-dd hh:ii:mm",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.end_time = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $('#datetimepicker1').datetimepicker({
            format: "yyyy-MM-dd hh:ii:mm",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.end_time = ev.date.valueOf();
            // console.log($scope.end_time);
            //console.log(ev.date.valueOf());
        });
        $scope.add=function(upontemplateall){
         console.log(upontemplateall)
        $scope.upontemplateall.promulgutor='';
        UserService.updatacoupontemplateall(upontemplateall, function (data) {
            alert("添加成功 ！");
            $state.go("usercenter.user");
           /* user.userid=data.userid;
            $state.go("usercenter.user");*/
            // history.go(-1);
        }, function (data) {
            alert("添加失败！"+data.data.msg);
            /*return;*/
        });
    }
        $scope.page={
            pageIndex:2,
            pageSize:2,
        }
        UserService.getcoupontemplateall($scope.page).$promise.then(function(data){
            console.log(data)
        })

}])
.controller('PreferentialAuthController', ['$scope','UserService','UserMemberService','user_list', function($scope, UserService,UserMemberService,user_list) {
       $scope.users=user_list;
       prepare_user($scope.users,0);
       for(var i=0;i<$scope.users.length;i++){
            $scope.picSrc = $scope.users[i].picAuth.split('{{img:');
            $scope.users[i].images=$scope.picSrc;
       }
     
       function prepare_user(list, i) {
            if (i >= list.length) {
                return;
            }
            var item = list[i];
            //console.log('getcountybyname: '+item.county);
            UserService.getuserbyid(item.userid).$promise.then(function (data) {
                item.name = data.name;
                item.phone=data.phone;
                item.nickname=data.nickname;
                prepare_user(list, i + 1);
            })
        }

        $scope.update=function(user,auth,sign){
            user.userAuth=auth;
            UserMemberService.update(user.userid,user,function(){
                alert("更新成功");
                window.location.reload();
            },function(){

            })
           
            user.sign=sign;
            UserService.update(user.userid,user,function(){

            },function(){

            })
        }
}])
;